package x.y.z;

public class Interview_4_10_CheckSubtree {
    // 先序遍历是否为子串
    public boolean checkSubTree(TreeNode t1, TreeNode t2) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        getPreOrderString(t1, sb1);
        getPreOrderString(t2, sb2);
        return sb1.indexOf(sb2.toString()) != -1;
    }

    public void getPreOrderString(TreeNode t1, StringBuilder sb) {
        if (t1 == null) {
            sb.append('X');
            return;
        }
        sb.append(t1.val);
        getPreOrderString(t1.left, sb);
        getPreOrderString(t1.right, sb);
    }
}
